const db=wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userid:"",
    starttime:'',
    endtime:'',
    starttime1:'',
    endtime1:'',
    show: false,
    showtishi:false,
    id:'',
    tixing:'',
    currentDate: '12:00',
    minHour: 0,
    maxHour: 24,
    taskname:'',
    taskdetails:'',
    tasktarget:'',
    targetnum:0,
  },
  onInput(event) {
    this.setData({
      tixing:"",
    })
    this.setData({
      currentDate: event.detail,
    });
    this.setData({
      tixing:this.data.tixing.concat(this.data.currentDate),
      showtishi:false
    })
  },
  onselect() {
    this.setData({ showtishi: true });
  },

  onClosetishi() {
    this.setData({ showtishi: false });
  },
  start() {
    this.setData({ 
      show: true, 
      id:"1" 
    });

  },
  end(){
    this.setData({ 
      show: true,
      id:"2"
    });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}/${date.getDate()}`;
    
  },
  onConfirm(event) {
    
    if(this.data.id=='1'){
      this.setData({
        starttime:"",
      })
  
      this.setData({
        show: false,
      });
      this.setData({
        starttime:event.detail,
        starttime1:this.data.endtime.concat(this.formatDate(event.detail))
      })
    }else{
      this.setData({
        endtime:"",
      })
      this.setData({
        show: false,
      });
      this.setData({
        endtime1:this.data.endtime.concat(this.formatDate(event.detail)),
        endtime:event.detail
      })
    }
    
  },

  chuancan(){
    let target=this.data.tasktarget.split(",");
    var addtask={"taskname":this.data.taskname,"taskpre":0,tasktarget:target,"starttime":this.data.starttime,"endtime":this.data.endtime,"tasktargetnum":this.data.targetnum,"hid":"block","value":this.data.taskdetails};
    if(this.data.taskname==''){
      wx.showToast({
        title: '任务名不能为空',
        icon:"error"
      })
    }else if(this.data.starttime==''){
      wx.showToast({
        title: '开始时间不能为空',
        icon:"error"
      })
    }else if(this.data.endtime==""){
      wx.showToast({
        title: '结束时间不能为空',
        icon:"error"
      })
    }else if(this.data.tasktarget==''){
      wx.showToast({
        title: '至少一个标签',
        icon:"error"
      })
    }else{
      db.collection("task").add({
        data:{
          allperson:1,
          finshedperson:0,
          hid:"block",
          endtime:addtask.endtime,
          starttime:addtask.starttime,
          target:target,
          taskname:addtask.taskname,
          connection:[],
          userid:this.data.userid
        }
      }).then(res=>{
        console.log(res)
      })
      wx.switchTab({
        url: '../task/task',
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
    wx.cloud.callFunction({
      name:"openid"
    }).then(res=>[
      this.setData({
        userid:res.result.openid
      })
    ])
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})